package com.wangjiyuan.shiwupai.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wangjiyuan.shiwupai.R;
import com.wangjiyuan.shiwupai.bean.guangchi.food.FeedsBean;
import com.wangjiyuan.shiwupai.ui.activity.detail.DetailActivity;
import com.wangjiyuan.shiwupai.utils.RecyclerViewAdapterHelper;

import java.util.List;

/**
 * Created by wjy on 2016/11/30.
 */

public class FoodAdapter extends RecyclerViewAdapterHelper<FeedsBean> {

    public FoodAdapter(Context context, List list) {
        super(context, list);
    }

    @Override
    public int getItemViewType(int position) {
        return mList.get(position).getContentType();
    }

    @Override
    public RecyclerView.ViewHolder onCreateMyViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        if (viewType == 1) {
            View view = mInflater.inflate(R.layout.item_knowledge_1, parent, false);
            viewHolder = new ViewHolder_1(view);
        } else {
            View view = mInflater.inflate(R.layout.item_food_2, parent, false);
            viewHolder = new ViewHolder_2(view);

        }
        return viewHolder;
    }

    @Override
    public void onBindMyViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder_1) {
            ((ViewHolder_1) holder).newInstance();
        } else if (holder instanceof ViewHolder_2) {
            ((ViewHolder_2) holder).newInstance();
        }
    }

    class ViewHolder_1 extends RecyclerView.ViewHolder {
        private TextView title, source, tail;
        private ImageView image;

        public ViewHolder_1(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.title);
            source = (TextView) itemView.findViewById(R.id.source);
            tail = (TextView) itemView.findViewById(R.id.tail);
            image = (ImageView) itemView.findViewById(R.id.image);
        }

        public void newInstance() {
            final FeedsBean feedsBean = mList.get(getAdapterPosition());
            title.setText(feedsBean.getTitle() + "");
            tail.setText(feedsBean.getTail() + "");
            source.setText(feedsBean.getSource() + "");
            Glide.with(mContext)
                    .load(feedsBean.getImages().get(0))
                    .fitCenter()
//                    .placeholder(R.drawable.img_home_default)
                    .into(image);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(mContext, DetailActivity.class);
                    intent.putExtra("link", feedsBean.getLink());
                    mContext.startActivity(intent);
                }
            });
        }
    }

    class ViewHolder_2 extends RecyclerView.ViewHolder {
        private TextView title, source, tail;
        private ImageView image1, image2, image3;

        public ViewHolder_2(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.title);
            source = (TextView) itemView.findViewById(R.id.source);
            tail = (TextView) itemView.findViewById(R.id.tail);
            image1 = (ImageView) itemView.findViewById(R.id.image1);
            image2 = (ImageView) itemView.findViewById(R.id.image2);
            image3 = (ImageView) itemView.findViewById(R.id.image3);
        }

        public void newInstance() {
            final FeedsBean feedsBean = mList.get(getAdapterPosition());
            title.setText(feedsBean.getTitle() + "");
            tail.setText(feedsBean.getTail() + "");
            source.setText(feedsBean.getSource() + "");
            Glide.with(mContext)
                    .load(feedsBean.getImages().get(0))
                    .fitCenter()
//                    .placeholder(R.drawable.img_home_default)
                    .into(image1);
            Glide.with(mContext)
                    .load(feedsBean.getImages().get(1))
                    .fitCenter()
//                    .placeholder(R.drawable.img_home_default)
                    .into(image2);
            Glide.with(mContext)
                    .load(feedsBean.getImages().get(2))
                    .fitCenter()
//                    .placeholder(R.drawable.img_home_default)
                    .into(image3);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(mContext, DetailActivity.class);
                    intent.putExtra("link", feedsBean.getLink());
                    mContext.startActivity(intent);
                }
            });
        }
    }
}
